home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11192 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.9 KB

  1. Path: coranto.ucs.mun.ca!usenet
  2. From: saustin@terra.nlnet.nf.ca (Steve Austin)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: A question of (lexical) style
  5. Date: Wed, 13 Mar 1996 04:20:26 GMT
  6. Organization: Kickham Productions
  7. Message-ID: <4i5ie1$att@coranto.ucs.mun.ca>
  8. References: <313c99bc.0@193.89.47.9>
  9. NNTP-Posting-Host: n104h129.nlnet.nf.ca
  10. X-Newsreader: Forte Agent .99b.112
  11.  
  12. Brian Elgaard <elgaard@dk-online.dk> wrote:
  13.  
  14. >A question of (lexical) style
  15. >
  16. >I am currently writing guidelines for C++ programmers, and I am in doubt about how to place * and & in declarations.
  17.  
  18. I've commented on this elsewhere, but I feel this is the source of
  19. much of the confusion that newcomers to C/C++ feel when presented with
  20. pointers and references. Textbooks often describe the declaration
  21.  
  22.  1.   int *ip;
  23.  
  24. as declaring ip as a pointer to an int. This leaves some people with
  25. the erroneous impression that *ip is a pointer to an int, so that they
  26. become confused about the use of the * operator. Placing the *
  27. modifier next to the type being modified seems more logical, and
  28. avoids this confusion.
  29.  
  30. Now, K&R read (1) above as declaring *ip to be an integer (Chapter 5,
  31. p90). Although *ip is obviously an int, looking at the declaration
  32. this way can lead the unwary into thinking that
  33.  
  34.   2.  int *ip = i;    /* where i is an int */
  35.  
  36. is legal C. After all *ip is an int isn't it? Trouble is, (1) isn't
  37. declaring the int *ip, it's declaring a pointer, ip.
  38.  
  39. This potential for confusion is exacerbated for those new to C++ when
  40. they see
  41.  
  42.   3.  int &ir = i;
  43.  
  44. Now, &ir is *not* an integer. Perhaps that's why Stroustrup places *
  45. and & next to the type rather than the object.
  46.  
  47. Maybe it's the multiple declaration construct which causes the trouble
  48. here. When you think about it, the ability to declare objects of
  49. different types on the same line is rather weird. I think it's come to
  50. seem natural through familiarity, but I do think it adds to a
  51. beginners confusion surrounding pointers.
  52.  
  53. Go on, flame me....
  54.  
  55. Steve
  56.  
  57.  
  58.  
  59.